<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="SPU" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="产品类型" prop="product_type">
                    <el-select v-model="queryParams.product_type" placeholder="请选择产品类型" clearable style="width: 240px">
                        <el-option v-for="(dict, index) in productTypeList" :key="index" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="运营" prop="admin_id">
                    <el-select v-model="queryParams.admin_id" placeholder="请选择运营" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in salesmanList" :key="index" :label="dict.nickname" :value="dict.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="达人名" prop="expert_name">
                    <el-input v-model.trim="queryParams.expert_name" placeholder="请输入达人名" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                
                <el-form-item label="广告平台" prop="platform_id">
                    <el-select v-model="queryParams.material_type" placeholder="请选择广告平台" clearable size="small" style="width: 240px">
                        <el-option v-for="(item, index) in platformList" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="素材类型" prop="material_type">
                    <el-select v-model="queryParams.material_type" placeholder="请选择素材类型" clearable size="small" style="width: 240px">
                        <el-option v-for="(item, index) in materialType" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({}, 'add')">添加</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="店铺" prop="shop_name" min-width="120" show-overflow-tooltip></el-table-column>
                <el-table-column label="spu" prop="spu" min-width="160" show-overflow-tooltip></el-table-column>
                <el-table-column label="产品类型" prop="product_type" width="120" show-overflow-tooltip></el-table-column>
                <el-table-column label="素材地址" prop="material_url" min-width="200" show-overflow-tooltip></el-table-column>
                <el-table-column label="达人" prop="expert_name" min-width="120" show-overflow-tooltip></el-table-column>
                <el-table-column label="素材分类" prop="material_type" min-width="120" show-overflow-tooltip>
                    <template slot-scope="scope">
                        {{ scope.row.material_type | formatLabelByValueFuzzy(materialType) }}
                    </template>
                </el-table-column>
                <el-table-column label="操作人" prop="operater" width="120" show-overflow-tooltip></el-table-column>
                <el-table-column label="创建时间" prop="create_time" width="160"></el-table-column>
                <el-table-column label="修改时间" prop="update_time" width="160"></el-table-column>

                <el-table-column label="操作" min-width="180" fixed="right">
                        <template slot-scope="scope">
                            <el-button type="text" icon="el-icon-download" @click="showDetailDialog(scope.row, 'edit')">下载</el-button>
                            <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row, 'edit')">编辑</el-button>
                            <el-button type="text" icon="el-icon-delete" @click="deleteRow(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>

        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
    </div>
</template>

<script>
import { getAttrList } from '@/api/product'
import { materialList, queryAdvplatformList, proxyownershipList } from '@/api/advertisement'
import { getShopList, getSalesmanList } from '@/api/shop'
import { STATUS_LIST, MATERIAL_PLATFORM_LIST, MATERIAL_TYPE_LIST, PRODUCT_TYPE_LIST } from '../constant'
import DetailDialog from './components/DetailDialog.vue'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'AdvaccountList',
    components: { DetailDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 店铺列表下拉
            shopList: [],
            salesmanList: [],
            platformList: MATERIAL_PLATFORM_LIST,
            ProxyOwnershipList: [],
            productTypeList: PRODUCT_TYPE_LIST,
            cooperateType: [],
            materialType: MATERIAL_TYPE_LIST,
            STATUS_LIST,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                shop_id: undefined,
                admin_id: undefined,
                advertiser_id: undefined,
                platform_id: undefined,
            },
            detailDialog: {
                visible: false,
                data: {},
            },
        }
    },
    watch: {
        $route: {
            handler(val) {
                this.queryParams.advertiser_id = val.query.advertiser_id
            },
            immediate: true,
        },
    },
    async created() {
        this.loading = true
        try {
            const [shopData, salesmanData, PlatformData, ProxyOwnershipData, productTypeData] = await Promise.all([
                getShopList(),
                getSalesmanList(),
                queryAdvplatformList(),
                proxyownershipList(),
                getAttrList(),
            ])
            this.shopList = shopData?.data?.list ?? []
            this.salesmanList = salesmanData?.data?.list ?? []
            // this.platformList = PlatformData?.data?.list ?? []
            this.ProxyOwnershipList = ProxyOwnershipData?.data?.list ?? []
            this.tt = productTypeData?.data ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
                deleteRow(row) {
            this.$confirm('是否确认删除该数据?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    delPlan({ id: row.id }).then(res => {
                        this.$message.success('删除成功')
                        this.getCarPlan()
                    })
                })
                .catch(() => {})
        },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        toAscription(row) {
            this.$router.push({
                path: '/advertisement/ascription',
                query: {
                    advertiser_id: row.advertiser_id,
                },
            })
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            materialList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style scoped></style>
